package com.cy.pj.sys.web.controller;

import com.cy.pj.sys.pojo.SysNotice;
import com.cy.pj.sys.service.SysNoticeService;
import com.cy.pj.sys.web.pojo.JsonResult;
import com.github.pagehelper.ISelect;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/notice/")
public class SysNoticeController {

    @Autowired
    private SysNoticeService sysNoticeService;

    @GetMapping
    public JsonResult doFindNotices(SysNotice sysNotice){
//        PageInfo<SysNotice> pageInfo= PageHelper.startPage(1,3).doSelectPageInfo(new ISelect() {
//            @Override
//            public void doSelect() {
//                sysNoticeService.findNotices(sysNotice);
//            }
//        });
        PageInfo<SysNotice> pageInfo=PageHelper.startPage(1,3).
                doSelectPageInfo(()->sysNoticeService.findNotices(sysNotice));
        return new JsonResult(pageInfo);
    }

    @PostMapping
    public JsonResult doSaveNotice(@RequestBody SysNotice notice){
        sysNoticeService.saveNotice(notice);
        return new JsonResult("save ok");
    }

    @GetMapping("{id}")
    public JsonResult doFindById(@PathVariable Long id){
        return new JsonResult(sysNoticeService.findById(id));
    }

    @PutMapping
    public JsonResult doUpdateNotice(@RequestBody SysNotice notice){
        sysNoticeService.updateNotice(notice);
        return new JsonResult("update ok");
    }

    @DeleteMapping("{ids}")
    public JsonResult doDeleteById(@PathVariable Long ids){
        sysNoticeService.deleteById(ids);
        return new JsonResult("delete ok");
    }
}
